<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        *{
            padding: 0;
            margin: 0;
            box-sizing: border-box;
        }
        ul{
            list-style: none;
            display: flex;
            width: 1200px;
            height: 100px;
            margin: 0 auto;
            background-color: #ccc;
            margin-top: 50px;
        }
        ul li{
            width: 100%;
            height: 100%;
            text-align: center;
            overflow: hidden;
        }
        ul li.active{
            overflow: visible;
        }
        ul li span{
            display: inline-block;
            width: 100%;
            height: 100%;
            line-height: 100px;
            border-right: 1px solid #aaa;
            transition: all 0.5s;
        }
        ul li span.on{
            background-color: pink;
            color: orange;
            transform: scale(1.2);
        }
        ul li .second{ 
            width: 100%;
            height: 200px;
            background-color: aqua;
        }
        ul li .second div{
            height: 50px;
            width: 100%;
            text-align: center;
            line-height: 49px;
            border-bottom: 1px solid #aaa;
        }
    </style>
</head>
<body>
    <ul>
        <li><span>顶部·1</span>
            <div class="second">
                <div>子菜单1</div>
                <div>子菜单2</div>
                <div>子菜单3</div>
                <div>子菜单4</div>
            </div>
        </li>
        <li><span>顶部·2</span>
            <div class="second">
                <div>子菜单1</div>
                <div>子菜单2</div>
                <div>子菜单3</div>
                <div>子菜单4</div>
            </div>
        </li>
        <li><span>顶部·3</span>
        </li>
        <li><span>顶部·4</span>
            <div class="second">
                <div>子菜单1</div>
                <div>子菜单2</div>
                <div>子菜单3</div>
                <div>子菜单4</div>
            </div>
        </li>
        <li><span>顶部·5</span>
        </li>
        <li><span>顶部·6</span>
            <div class="second">
                <div>子菜单1</div>
                <div>子菜单2</div>
                <div>子菜单3</div>
                <div>子菜单4</div>
            </div>
        </li>
    </ul>

    <script>
        //需求：移入导航栏的时候，如果他有子菜单，就会自动显示，如果没有就不显示，并且改变样式
        let spans=document.querySelectorAll("ul li span")
        let lis=document.querySelectorAll("ul li")
        let divs=document.querySelectorAll("ul li .second")
        lis.forEach((i,j) => {
            if (i.children.length==1) {
                return
            }
            i.addEventListener("mouseenter",function(){
                spans[j].classList.add("on")
                i.classList.add("active")
            })
            i.addEventListener("mouseleave",function(){
                spans[j].classList.remove("on")
                i.classList.remove("active")
            })
        });
    </script>
</body>
</html>